<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;

class Preject extends Controller{
    public function __construct(){
        parent::__construct();
        if (!Session::has('admin')){
            $this->redirect('index/login');
        }


    }
    //查出项目
    public function preject(){
        if (Session::get('apartment')!==''){
            $preject=Db::table('preject')->where('apartment',Session::get('apartment'))->paginate(30);
            $this->assign('preject',$preject);
        }else{
            $preject=Db::table('preject')->order('time', 'desc')->paginate(30);
            $this->assign('preject',$preject);
        }

        return view();
    }
    //查出公告
    public function notice(){
        $notice=Db::table('notice')->order('time', 'desc')->paginate(30);
        $this->assign('notice',$notice);
        return view();
    }
    //查出问题
    public function question(){
        $question=Db::table('question')->order('time', 'desc')->paginate(30);
        $this->assign('question',$question);
        return view();
    }
    //删除项目
    public function deletepreject(){
        $id=input('get.id');
        Db::table('person')->where('uid',$id)->delete();
        Db::table('team')->where('uid',$id)->delete();
        Db::table('preject')->where('id',$id)->delete();
        $p=$_SERVER["HTTP_REFERER"];
        $this->redirect($p);

    }

    //删除公告
public function deletenotice(){
    $id=input('get.id');
    Db::table('notice')->where('id',$id)->delete();
    $p=$_SERVER["HTTP_REFERER"];
    $this->redirect($p);

}
    //删除问题
    public function deletequestion(){
        $id=input('get.id');
        Db::table('question')->where('id',$id)->delete();
        $p=$_SERVER["HTTP_REFERER"];
        $this->redirect($p);
    }
    //发布项目
    public function sendpreject(){
      if (request()->isPost()){
          $data['title']=input('post.title');
          $data['apartment']=input('post.apartment');
          $data['endtime']=input('post.endtime');
          $data['content']=input('post.content');
          if ($data['title']==""){
              $this->error('标题不能为空');
          }elseif ($data['apartment']==""){
              $this->error('部门不能为空');
          }else if ($data['content']==""){
              $this->error('内容不能为空');
          }
          Db::name('preject')->insert($data);
          $this->redirect('preject');
      }
      if (request()->isGet()){
        $preject=Db::table('preject')->where('id',input('get.id'))->find();
        $this->assign('preject',$preject);
      }
        return view();
    }


    //发布公告
    public function sendnotice(){
        if (request()->isPost()){
            $data['title']=input('post.title');
            $data['content']=input('post.content');
            if ($data['title']==""){
                $this->error('标题不能为空');
            }elseif($data['content']==""){
                $this->error('内容不能为空');
            }
            Db::name('notice')->insert($data);
            $this->redirect('notice');
        }


        if (request()->isGet()){
            $notice=Db::table('notice')->where('id',input('get.id'))->find();
            $this->assign('notice',$notice);

        }

        return view();
    }
    //发布问题
    public function sendquestion(){
        if (request()->isPost()){
            $data['title']=input('post.title');
            $data['content']=input('post.content');
            if ($data['title']==""){
                $this->error('标题不能为空');
            }elseif($data['content']==""){
                $this->error('内容不能为空');
            }
             Db::name('question')->insert($data);
            $this->redirect('question');
        }

        if (request()->isGet()){
            $question=Db::table('question')->where('id',input('get.id'))->find();
            $this->assign('question',$question);
        }


        return view();
    }

    //更新项目
    public function updatepreject(){
        if (request()->isPost()){
            $data['title']=input('post.title');
            $data['apartment']=input('post.apartment');
            $data['endtime']=input('post.endtime');
            $data['content']=input('post.content');
            Db::name('preject')->where('id',input('post.id'))->update($data);
            $this->redirect('preject');
        }
    }

    //更新公告
    public function updatenotice(){
        if (request()->isPost()){
            $data['title']=input('post.title');
            $data['content']=input('post.content');
            Db::name('notice')->where('id',input('post.id'))->update($data);
            $this->redirect('notice');
        }
    }

    //更新问题
    public function updatequestion(){
        if (request()->isPost()){
            $data['title']=input('post.title');
            $data['content']=input('post.content');
            Db::name('question')->where('id',input('post.id'))->update($data);
            $this->redirect('question');
        }
    }

}